/**
*  \file switch.c
*
*  \brief
*
*  \author Dajin Li
*
*
*/
/*===============================================[ private includes  ]================================================*/

#include "switch.h"



/*===============================================[ private defines   ]================================================*/
#define SWITCH_SAMPLES                       30				// SAMPLES * execution rate = debounce time
#define SWITCH_PORT                          GPIOA
#define SWITCH_RD_PIN                        GPIO_Pin_11
#define SWITCH_CLK_PIN                       GPIO_Pin_10
#define SWITCH_LOAD_PIN                      GPIO_Pin_9
#define SWITCH_INPUTS_NUM                    16
#define AIR_FILTER_FAIL_IN                   BIT0   // P20, normal high, air cleaner failure signal input
#define FUEL_CAPACITY_LOW_IN                 BIT2   // P22, normal high, fuel capacity empty alarm signal input
#define HYD_TEMP_HOT_IN                      BIT3   // P30, normal high, hydraulic oil over temp signal input
#define ENG_CHK_IN                           BIT4   // P11, normal high, check engine signal input
#define OIL_PRESSURE_LOW_IN                  BIT5   // P12, normal high, oil pressure low signal input
#define ENG_TEMP_HOT_IN                      BIT6   // P14, normal high, engine overheat signal input
#define LOW_BRAKE_OIL_SIGNAL_IN              BIT7   // P15, normal high, low brake oil signal
#define WATER_SEPARATOR_FUEL_FILTER_IN       BIT8   // P34, normal high, water separator fuel filter signal input, graphical area
#define PREHEAT_IN                           BIT9   // P33, normal high, preheat signal input, graphical area
#define PARTICLE_FILTER_PREMOTION_SIGNAL_IN  BIT10  // P32, normal high, particle filter premotion signal input, graphical area
#define SAFETY_BELT_IN                       BIT11  // P28, normal high, seat belt switch signal input, graphical area
#define ID_SYSTEM_IN                         BIT12  // P27, normal high, electric error signal input
#define PARKING_BRAKE_SIGNAL_IN              BIT13  // P26, normal high, parking brake signal input
#define DIRECTION_RIGHT_SIGNAL_IN            BIT14  // P25, normal low, direction right signal input, grahpical area
#define DIRECTION_LEFT_SIGNAL_IN             BIT15  // P24, normal low, direction left signal input, graphical area 
#define CHARGE_FAIL_IN                       BIT16  // P19, normal low, charge failure input 


/*BR1285 input definiton ---start*/
#define LIFT_CUTBACK_IN                      BIT4   // P11---c7---input C7 Lift Cutback
#define STEERING_COLUMN_IN             		 BIT6   // P14---c8---input C8 Steer column active.
#define ACCEL_TYPE_CODING_IN               	 BIT5   // P12---c10---input C10 accel type coding
#define TRACTION_CUTBACK_IN              	 BIT7   // P15---b1---input B1 Traction Cutback
#define SEAT_BELT_IN                       	 BIT11  // P28---seat belt
/*BR1285 input definiton ---end*/
#define CHARGE_ERR_PORT                      GPIOB
#define CHARGE_ERR_PIN                       GPIO_Pin_0


#define ENG_CHK_LEVEL                        0
#define OIL_PRESSURE_LOW_IN_LEVEL            0
#define ENG_TEMP_HOT_IN_LEVEL                0



/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/
static u32 debounce_buff[SWITCH_SAMPLES + 1];	     // circular buffer
static unsigned int load_offset = 0;  			     // offset variable for loading buffer
static int debounce_time = SWITCH_SAMPLES;			 // Default debounce time = 1000ms
static int Switches_Ready_Count = 0;				 // Count to make sure we have valid switches at startup
//static const u32 switch_error_in = AIR_FILTER_VACUMN | FUEL_LEVEL_LOW | HYD_TEMP_HOT | CHK_ENG | ENG_OIL_PRESSURE_LOW | ENG_OIL_PRESSURE_LOW_SOUND_POPUP | MOTOR_TEMP_HOT 
 //                                  | BRAKE_OIL_LOW_SIGNAL | CHARGE_FAULT | ERROR_IN_ELEC | COOLANT_LEVEL_LOW;
/*===============================================[ public variables  ]================================================*/
u32 switches_current;
int Switches_Ready = 0;						// flag indicating sure we have valid switches at startup
u16 kwp_input = 0;
u16 kwp_input_1219 = 0;
/*================================================[ inline functions ]================================================*/





/*===============================================[ private functions ]================================================*/
static u32 get_switches(void);

#define INDICATOR_TIME	(62)

/*================================================[ public functions ]================================================*/
/**
* @brief	This samples the switch inputs.
* @param	none
* @note     none
* @retval   none
*/
void switch_inputs_task(void const *argument) 
{
    register unsigned int read_offset;      // offset variable for reading buffer
    u32 and_buff = 0xFFFFFFFF;          // variable for ANDding total buffer
    u32 or_buff = 0x00000000;           // variable for ORing total buffer

    // Initialization 
  //  task_status[SAMPLE_SW_INPUTS_ID] = ALIVE;
    debounce_time = SWITCH_SAMPLES;
    // Tasks must run in an infinite loop:
    while (TRUE) 
    {
        and_buff = 0xFFFFFFFF;
        or_buff = 0x00000000;
        
        debounce_buff[ load_offset ] = get_switches();                          // load buffer and rotate offset
        if(load_offset < debounce_time)
        {
            load_offset++;
        }
        else
        {
            load_offset = 0;
            if (++Switches_Ready_Count >= 2)
            {
                Switches_Ready = 1;
            }
        }

        for(read_offset = debounce_time; read_offset ; read_offset-- )
        {
            or_buff   |= debounce_buff[read_offset];        // OR all buffer entries
            and_buff  &= debounce_buff[read_offset];        // AND all  buffer entries
        }

        switches_current = (switches_current | and_buff) & or_buff;  // Get samples

        task_status[SAMPLE_SW_INPUTS_ID] = ALIVE;
        osDelay(SAMPLE_SW_INPUTS_ITV);
    }  // end while infinite loop
}  // end task_sample_sw_inputs()	 

/*===============================================[ private functions ]================================================*/

u8 key;
/**
* @brief	This samples the switch inputs.
* @param	none
* @note     none
* @retval   u16: swtich value
*/
u32 get_switches(void)
{
    u32 switch_inputs = 0;

    switch_inputs |= ((U32)GET_KEY1()) << 0;
    switch_inputs |= ((U32)GET_KEY2()) << 1;
    switch_inputs |= ((U32)GET_KEY3()) << 2;
    switch_inputs |= ((U32)GET_KEY4()) << 3;
	switch_inputs |= ((U32)GET_KEY_SWITCH()) << 4;
    key = switch_inputs;
    return switch_inputs;
}


/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

